
public class e035 {
    public static void main(String[] args) {
        long sTime = System.currentTimeMillis();
        int num = 0;
        for (int i = 2; i < 1000000; i++) {
            if (isCircPrime(i)) {
                num++;
            }
        }
        System.out.println(num);
        long fTime = System.currentTimeMillis();
        System.out.println("Time: " + (fTime - sTime) + "ms");
    }

    static boolean isCircPrime(int i) {
        if (!isPrime(i)) {
            return false;
        }
        String l = String.valueOf(i);
        StringBuilder b = new StringBuilder(l);
        for (int j = 0; j < l.length() - 1; j++) {
            l = b.toString();
            char c = l.charAt(0);
            b.deleteCharAt(0);
            b.append(c);
            if (!isPrime(Integer.parseInt(b.toString()))) {
                return false;
            }
        }
        return true;
    }

    static boolean isPrime(int i) {
        for (int j = 2; j <= Math.sqrt(i); j++) {
            if (i % j == 0) {
                return false;
            }
        }
        return true;
    }
}